/**
 * Created by leo on 2/2.
 */
//scope属性值为空代表创建一个隔离的作用域，代表不依赖外部的作用域。false代表自己所在的作用域true代表创建一个从负作用域继承的子作用域。
angular.module('panels')
    .directive('panelPortalQuickService', function (tabService, loadConf, Promise, LoginInfo,$http) {
        return {
            scope: {},

            link: function (scope) {
                alert("0000000");
                scope.clickService = function (service) {
                    tabService.addTab({'name': service.name, 'url': service.url});
                };
                Promise.when(LoginInfo).then(function (loginInfo) {
                    // 判断当前用户是否有权限访问该service
                    scope.isAvailable = function (service) {
                        if (service._available !== undefined) {
                            return service._available;
                        }
                        var available = true,
                            enabledRoles = service.roles,
                            currentRoles = loginInfo.currentUser.roles;
                        if (angular.isArray(enabledRoles)) {
                            angular.forEach(enabledRoles, function (roleId, idx) {
                                enabledRoles[idx] = '' + roleId;
                            });
                            available = false;
                            for (var i = 0; i < currentRoles.length; i++) {
                                available = enabledRoles.indexOf(currentRoles[i].ROLE_ID) != -1;
                                if (available) break;
                            }
                        }
                        service._available = available;
                        return available;
                    };
                });
                alert("11110");
                loadConf('panel-portal-quick-services.yml')
                    .then(function (dat) {
                        scope.serviceList = dat;
                    });
                alert("222222");

            },
            templateUrl: 'app/panel/portal/quick-service.html'
        };
    });
